package workflow

import (
	"beesoft.ink/application/media/app/workflow/activity"
	"go.temporal.io/sdk/workflow"
	"time"
)

type StructuredUnit struct {
}

func AudioSynthesisWorkflow(ctx workflow.Context, text string, voices []string) error {
	ao := workflow.ActivityOptions{
		StartToCloseTimeout: time.Minute,
	}
	ctx = workflow.WithActivityOptions(ctx, ao)

	var segments []string
	err := workflow.ExecuteActivity(ctx, activity.AudioSynthesisSplitTextActivity, text).Get(ctx, &segments)
	if err != nil {
		return err
	}

	var structuredData []StructuredUnit
	err = workflow.ExecuteActivity(ctx, activity.AudioSynthesisGenerateStructuredDataActivity, segments, voices).Get(ctx, &structuredData)
	if err != nil {
		return err
	}

	var groupedData [][]StructuredUnit
	err = workflow.ExecuteActivity(ctx, activity.AudioSynthesisGroupDataActivity, structuredData).Get(ctx, &groupedData)
	if err != nil {
		return err
	}

	var ssmlData []string
	err = workflow.ExecuteActivity(ctx, activity.AudioSynthesisGenerateSsmlActivity, groupedData).Get(ctx, &ssmlData)
	if err != nil {
		return err
	}

	var audioFiles []string
	err = workflow.ExecuteActivity(ctx, activity.AudioSynthesisAudioSynthesisActivity, ssmlData).Get(ctx, &audioFiles)
	if err != nil {
		return err
	}

	var finalAudio string
	err = workflow.ExecuteActivity(ctx, activity.AudioSynthesisConcatenateAudioActivity, audioFiles).Get(ctx, &finalAudio)
	if err != nil {
		return err
	}

	return nil
}
